package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.aspect.annotation.MyLog;
import com.itheima.commen.Result;
import com.itheima.entiry.Employee;
import com.itheima.service.EmployeeService;
import com.sun.deploy.net.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public Result login(HttpServletRequest request, @RequestBody Employee employee) throws NoSuchAlgorithmException {
        String password = employee.getPassword();
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(qw);
        if (emp == null) {
            return new Result(0, null, "登录失败");
        }
        if (!(BCrypt.checkpw(password,emp.getPassword()))) {
            return new Result(0, emp, "登录失败");
        }
        if (emp.getStatus() == 0) {
            return new Result(0, emp, "账号禁用");
        }
        request.getSession().setAttribute("msg", emp);
        return new Result(1,emp,"登陆成功");
    }


    @PostMapping("/logout")
    public Result logout(HttpSession session){
        session.removeAttribute("msg");
        return new Result(1);
    }

    @PostMapping
    public Result add(@RequestBody Employee employee){
        String pw = employee.getPassword();
        String hashpw = BCrypt.hashpw(pw, BCrypt.gensalt());
        employee.setPassword(hashpw);
//        Employee msg = (Employee) session.getAttribute("msg");
//        Long id = msg.getId();
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);
        boolean save = employeeService.save(employee);
        if (save){
            return new Result(1,employee);
        }
        return new Result(0,employee);
    }
    @MyLog(value = "分页",name = "admin")
    @GetMapping("/page")
    public Result selectPage( Integer page, Integer pageSize, String name){
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper<>();
        qw.eq(name!=null,Employee::getName,name);
        Page<Employee> page1 = new Page<>(page, pageSize);
        if (name==null){
            Page<Employee> emp = employeeService.page(page1);
//            List<Employee> records = emp.getRecords();
//            long total = emp.getTotal();
//            Page<Employee> employeePage = new Page<>();
//            employeePage.setRecords(records);
//            employeePage.setTotal(total);
//            System.out.println(total);
            return new Result(1,emp,null);
        }
//        qw.likeRight(Employee::getName,name);
//                .likeLeft(Employee::getName,name);
        qw.like(Employee::getName,name);
        Page<Employee> emp = employeeService.page(page1, qw);
//        List<Employee> records = emp.getRecords();
//        long total = emp.getTotal();
//        Page<Employee> employeePage = new Page<>();
//        employeePage.setRecords(records);
//        employeePage.setTotal(total);
//        System.out.println(total);
//        return new Result(1,employeePage,null);
        return new Result(1,emp,null);

    }

    @PutMapping
    public Result update(@RequestBody Employee employee){
//        Long id = employee.getId();
//        Employee byId = employeeService.getById(id);
//        Integer status = employee.getStatus();
//        if (status==1){
//           byId.setStatus(0);
//        }
//        if (status==0){
//            byId.setStatus(1);
//        }
//        Integer status = byId.getStatus();
//        byId.setStatus(1-status);
//        Employee msg = (Employee) session.getAttribute("msg");
//        Long msgId = msg.getId();
//        byId.setUpdateUser(msgId);
//        boolean updateById = employeeService.updateById(byId);
//        if (updateById){
//            return new Result(1,byId);
//        }
//        return new Result(0,byId,"修改状态失败");
//        Employee msg = (Employee) session.getAttribute("msg");
//        Long msgId = msg.getId();
//        employee.setUpdateUser(msgId);
        boolean updateById = employeeService.updateById(employee);
        if (updateById){
            return new Result(1,employee);
        }
        return new Result(0,employee,"修改状态失败");
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable long id){
        Employee byId = employeeService.getById(id);
        if (byId!=null)
        return new Result(1,byId);
        return new Result(0,null,"无数据可修改");
    }

}
